package com.jerry.project;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;

public class InterestedServlet  extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
		
		JSONObject jsonobj=null;
		String id=null;
		String user=null;
		
		try {
			jsonobj=new JSONObject(reqToJSONString.reqToJSONString(req));
			if(jsonobj.has("id")){
				id=jsonobj.getString("id");
			}
			if(jsonobj.has("user")){
				user=jsonobj.getString("user");
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Key accountKey = KeyFactory.createKey("AccountKind", "account");
		Key eventKey = KeyFactory.createKey("EventKind", "event");
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		if((id!=null)&&(user!=null)){
			Filter userFilter = FilterOperator.EQUAL.of("account", user);
			Filter idFilter = FilterOperator.EQUAL.of("interestedEventID", id);
			Filter queryFilter = CompositeFilterOperator.and(userFilter,idFilter);
	        Query query = new Query("Account", accountKey).setFilter(queryFilter);
	        List<Entity> detect = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(100));
	        if(detect.size()==0){
	        	queryFilter = FilterOperator.EQUAL.of("id", id);
	        	query = new Query("Event", eventKey).setFilter(queryFilter);
	        	List<Entity> events = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(100));
	        	Integer interestedNumber=Integer.parseInt(events.get(0).getProperty("interestedNumber").toString())+1;
	        	events.get(0).setProperty("interestedNumber", interestedNumber);
	        	datastore.put(events.get(0));
	        	
		        Entity account = new Entity("Account", accountKey);
		        account.setProperty("interestedEventID", id);
		        account.setProperty("account", user);
		        datastore.put(account);
		        resp.getWriter().print("you like it now");
	        }
	        else{
	        	queryFilter = FilterOperator.EQUAL.of("id", id);
	        	query = new Query("Event", eventKey).setFilter(queryFilter);
	        	List<Entity> events = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(100));
	        	Integer interestedNumber=Integer.parseInt(events.get(0).getProperty("interestedNumber").toString())-1;
	        	events.get(0).setProperty("interestedNumber", interestedNumber);
	        	datastore.put(events.get(0));
	        	
	        	datastore.delete(detect.get(0).getKey());
	        	resp.getWriter().print("you dont like it now");
	        }
		}
    }
    
}
